#ifndef HIDREACTOR_H
#define HIDREACTOR_H

#include <QObject>
#include <sys/select.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <stdio.h>
#include "hiddevice.h"
#include <QThread>
#include <QMap>
#include <QSocketNotifier>

class HidReactor : public QObject
{
    Q_OBJECT
public:
    explicit HidReactor(QObject *parent = nullptr);
    ~HidReactor();
    int findDevices();

    void select();
    void start();
    void quit();

private:
    int getUsage(int fd);
    int setRWFlags(int fd);

    //用于usb热拔插
    void setupNotifier();
    void handleNetLinkMsg();
    void openDevice(QString devPath);
private:
    QMap<QString,HidDevice*> m_hidDevices;
    bool m_bRunning;
    QThread* m_pThread;

    int netLinkSocket;
    QSocketNotifier *m_pSockNotifier;

signals:
    void newDevice(QString devName,HidDevice* dev);
    void delDevice(HidDevice* dev);
};

#endif // HIDREACTOR_H
